{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model Quickload\n",
    "\n",
    "This notebook will show you how to quickly load a pretrained SqueezeNet model and test it on images of your choice in four main steps. \n",
    "\n",
    "1. Load the model\n",
    "2. Format the input\n",
    "3. Run the test\n",
    "4. Process the results\n",
    "\n",
    "The model used in this tutorial has been pretrained on the full 1000 class ImageNet dataset, and is downloaded from Caffe2's [Model Zoo](https://github.com/caffe2/caffe2/wiki/Model-Zoo). For an all around more in-depth tutorial on using pretrained models check out the [Loading Pretrained Models](https://github.com/caffe2/caffe2/blob/master/caffe2/python/tutorials/Loading_Pretrained_Models.ipynb) tutorial.  \n",
    "\n",
    "Before this script will work, you need to download the model and install it. You can do this by running:\n",
    "\n",
    "```\n",
    "sudo python -m caffe2.python.models.download -i squeezenet\n",
    "```\n",
    "\n",
    "Or make a folder named `squeezenet`, download each file listed below to it, and place it in the `/caffe2/python/models/` directory:\n",
    "* [predict_net.pb](https://download.caffe2.ai/models/squeezenet/predict_net.pb)\n",
    "* [init_net.pb](https://download.caffe2.ai/models/squeezenet/init_net.pb)\n",
    "\n",
    "Notice, the helper function *parseResults* will translate the integer class label of the top result to an English label by searching through the [inference codes file](inference_codes.txt). If you want to really test the model's capabilities, pick a code from the file, find an image representing that code, and test the model with it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top-1 Prediction: 985\n",
      "Top-1 Confidence: 0.982226848602\n",
      "\n",
      "Raw top 3 results: [array([985.0, 0.9822268486022949], dtype=object), array([309.0, 0.011943653225898743], dtype=object), array([946.0, 0.0048101237043738365], dtype=object)]\n",
      "The image contains a daisy with a 98.22268486022949 percent probability.\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "from __future__ import unicode_literals\n",
    "import numpy as np\n",
    "import operator\n",
    "# load up the caffe2 workspace\n",
    "from caffe2.python import workspace\n",
    "# choose your model here (use the downloader first)\n",
    "from caffe2.python.models import squeezenet as mynet\n",
    "# helper image processing functions\n",
    "import helpers\n",
    "\n",
    "##### Load the Model\n",
    "# Load the pre-trained model\n",
    "init_net = mynet.init_net\n",
    "predict_net = mynet.predict_net\n",
    "\n",
    "# Initialize the predictor with SqueezeNet's init_net and predict_net\n",
    "p = workspace.Predictor(init_net, predict_net)\n",
    "\n",
    "##### Select and format the input image\n",
    "# use whatever image you want (urls work too)\n",
    "# img = \"https://upload.wikimedia.org/wikipedia/commons/a/ac/Pretzel.jpg\"\n",
    "# img = \"images/cat.jpg\"\n",
    "# img = \"images/cowboy-hat.jpg\"\n",
    "# img = \"images/cell-tower.jpg\"\n",
    "# img = \"images/Ducreux.jpg\"\n",
    "# img = \"images/pretzel.jpg\"\n",
    "# img = \"images/orangutan.jpg\"\n",
    "# img = \"images/aircraft-carrier.jpg\"\n",
    "img = \"images/flower.jpg\"\n",
    "\n",
    "# average mean to subtract from the image\n",
    "mean = 128\n",
    "# the size of images that the model was trained with\n",
    "input_size = 227\n",
    "\n",
    "# use the image helper to load the image and convert it to NCHW\n",
    "img = helpers.loadToNCHW(img, mean, input_size)\n",
    "\n",
    "##### Run the test\n",
    "# submit the image to net and get a tensor of results\n",
    "results = p.run({'data': img})  \n",
    "\n",
    "##### Process the results\n",
    "# Quick way to get the top-1 prediction result\n",
    "# Squeeze out the unnecessary axis. This returns a 1-D array of length 1000\n",
    "preds = np.squeeze(results)\n",
    "# Get the prediction and the confidence by finding the maximum value and index of maximum value in preds array\n",
    "curr_pred, curr_conf = max(enumerate(preds), key=operator.itemgetter(1))\n",
    "print(\"Top-1 Prediction: {}\".format(curr_pred))\n",
    "print(\"Top-1 Confidence: {}\\n\".format(curr_conf))\n",
    "\n",
    "# Lookup our result from the inference list\n",
    "response = helpers.parseResults(results)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
